<?php
/*
                           _
                           \"-._ _.--"~~"--._
                            \   "            ^.    ___
                            /                  \.-~_.-~
                     .-----'     /\/"\ /~-._      /
                    /  __      _/\-.__\L_.-/\     "-.
                   /.-"  \    ( ` \_o>"<o_/  \  .--._\
                  /'      \    \:     "     :/_/     "`
                          /  /\ "\    ~    /~"
                          \ I  \/]"-._ _.-"[
                       ___ \|___/ ./    l   \___   ___
                  .--v~   "v` ( `-.__   __.-' ) ~v"   ~v--.
               .-{   |     :   \_    "~"    _/   :     |   }-.
              /   \  |           ~-.,___,.-~           |  /   \
             ]     \ |                                 | /     [
             /\     \|     :                     :     |/     /\
            /  ^._  _K.___,^                     ^.___,K_  _.^  \
           /   /  "~/  "\                           /"  \~"  \   \
          /   /    /     \ _          :          _ /     \    \   \
        .^--./    /       Y___________l___________Y       \    \.--^.
        [    \   /        |        [/    ]        |        \   /    ]
        |     "v"         l________[____/]________j         }r"     /
        }------t          /                       \       /`-.     /
        |      |         Y                         Y     /    "-._/
        }-----v'         |         :               |     7-.     /
        |   |_|          |         l               |    / . "-._/
        l  .[_]          :          \              :  r[]/_.  /
         \_____]                     "--.             "-.____/




        */
/**
 * Created by PhpStorm.
 * User: hengge
 * Date: 2018/6/2
 * Time: 15:11
 */
namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        config('default_ajax_return','json');



    }

    //登录页面
    public function index(){
        //登录检测
        if(Session::has('admin.username')){
            $this->redirect('admin/index/index');
        }

        return $this->fetch();
    }

    //登录提交
    public function login(){

        if($this->request->isPost()){
            $param=$this->request->param();
            $arr=['code'=>0,'msg'=>'登录失败'];
            if(!captcha_check($param['captcha'])){
                $arr['msg']='验证码错误';
                return $arr;
            }
            $data=Db::name('admin')->where(['username'=>$param['username']])->find();
            if(empty($data) || !Lemon_check_pwd($param['password'],$data['password'])){
                $arr['msg']='用户名或密码错误';
                return $arr;
            }
            session('admin',$data);
            $arr=['code'=>200,'msg'=>'登录成功'];
            return $arr;
        }
    }

    //登录退出
    public function logout(){
        session('admin',null);
        $arr=['code'=>200,'msg'=>'退出登录'];
        return $arr;
    }
}